﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using ELMApplication.Common.Model;


using ELMApplication.Common.Const;
using System.Linq;
using Microsoft.Practices.Composite.Events;
using ELMApplication.Common.Events;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Composite.Regions;

using ELMWCFServiceClass.ELMWCF;


namespace DataflowListModule.Views.DataflowListView
{
    public class DataflowListPrentationModel : IDataflowListPresentationModel
    {
        private ELMServiceClient _domaincontext;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager regionManager;
        private readonly IModuleManager moduleManager;
        public DataflowListPrentationModel(IDataflowListView view, ELMServiceClient domaincontext, IEventAggregator eventAggregator, IRegionManager regionManager, IModuleManager moduleManager)
        {
            this._domaincontext = domaincontext;
            this._eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.moduleManager = moduleManager;
            AllTaskdata = new ObservableCollection<TaskData>();
            
            
            this.View = view;
            _eventAggregator.GetEvent<TempEvent>().Subscribe(loadData,true);
            view.SetModel(this);
        }
        private void loadData(Dataflow df)
        {
            BindList();
        }

        public void Refresh()
        {
            if (Current.User != null)
            {

                BindList();
            }
        }
        private void BindList()
        {
            
            _domaincontext.GetTastDataWhichUserUnreadedAsync(Current.User.HumanId);
            _domaincontext.GetTastDataWhichUserUnreadedCompleted += (sender, e) =>
                {
                    if (e.Error == null)
                    {
                        AllTaskdata = e.Result;
                    }
                };
            _domaincontext.GetTastDataWhichUserReadedAsync(Current.User.HumanId);
            _domaincontext.GetTastDataWhichUserReadedCompleted += (sender, e) =>
                {
                    if (e.Error == null)
                    {
                        ReadedTaskdata = e.Result;
                    }
                };
            _domaincontext.GetTastDataWhichUserSendedAsync(Current.User.HumanId);
            _domaincontext.GetTastDataWhichUserSendedCompleted += (sender, e) =>
                {
                    if (e.Error == null)
                    {
                        SendedTaskdata = e.Result;
                    }
                };

        }

       
        public void GetDetail(int TaskDetailId)
        {
            var data = AllTaskdata.Single(c => c.ID == TaskDetailId);
            moduleManager.LoadModule("DataFlowModule");
            _eventAggregator.GetEvent<TaskDataEvent>().Publish(data);
            //regionManager.Regions["MainRegion"].Remove(View);
            _domaincontext.SetIsReadedAsync(TaskDetailId);
            _domaincontext.SetIsReadedCompleted += (sender,e) =>
                {
                    if (e.Error == null)
                    {
                        BindList();
                    }
                };
            NotifyPropertyChanged("ISOK");
            
        }
        public void GetDetail1(int TaskDetailId)
        {
            var data = ReadedTaskdata.Single(c => c.ID == TaskDetailId);
            moduleManager.LoadModule("DataFlowModule");
            _eventAggregator.GetEvent<TaskDataEvent>().Publish(data);
           // regionManager.Regions["MainRegion"].Remove(View);
            NotifyPropertyChanged("ISOK");

        }
        public void GetDetail2(int TaskDetailId)
        {
            var data = SendedTaskdata.Single(c => c.ID == TaskDetailId);
            moduleManager.LoadModule("DataFlowModule");
            _eventAggregator.GetEvent<TaskDataEvent>().Publish(data);
            //regionManager.Regions["MainRegion"].Remove(View);
            NotifyPropertyChanged("ISOK");

        }




        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region IDataflowListPresentationModel Members

        public IDataflowListView View
        {
            get;
            set;
        }



        #endregion

        #region IDataflowListPresentationModel Members

        private ObservableCollection<TaskData> _taskData;
        public ObservableCollection<TaskData> AllTaskdata
        {
            get
            {
                return _taskData;
            }
            set
            {
                if (_taskData != value)
                {

                    _taskData = value;
                    NotifyPropertyChanged("AllTaskdata");

                }
                
            }
        }

        #endregion

        #region IDataflowListPresentationModel 成员

        private TaskData _selecteddata;
        public TaskData SelectedTaskData
        {
            get
            {
                return _selecteddata;
            }
            set
            {
                if (_selecteddata != value)
                {
                    _selecteddata = value;
                    NotifyPropertyChanged("SelectedTaskData");
                    MessageBox.Show("fwefw");
                }
            }
        }

        #endregion

        #region IDataflowListPresentationModel Members

        private ObservableCollection<TaskData> _readedTaskdata;
        public ObservableCollection<TaskData> ReadedTaskdata
        {
            get
            {
                return _readedTaskdata;
            }
            set
            {
                _readedTaskdata=value;
                NotifyPropertyChanged("ReadedTaskdata");
            }
        }
        private ObservableCollection<TaskData> _sendedTaskdata;
        public ObservableCollection<TaskData> SendedTaskdata
        {
            get
            {

                return _sendedTaskdata;     
            }
            set
            {
                _sendedTaskdata=value;
                NotifyPropertyChanged("SendedTaskdata");            
        }}

        #endregion
    }
}
